package com.dys;

import com.dys.dao.UserDAO;
import com.dys.dao.impl.UserDAOImpl;
import com.dys.entity.User;

import java.sql.SQLException;
import java.util.List;

/**
 * @className: com.dys.ApacheUtilsTest
 * @description: Commons DBUtils的工具，是Apache组织提供的一个对JDBC进行简单封装的开源工具类库。
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/16 下午12:59
 */
public class ApacheUtilsTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws SQLException {
        testSelectCount();
    }

    /**
     * 测试添加User
     *
     * @throws SQLException
     */
    public static void testAdd() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        User user = new User();
        user.setUsername("范冰冰");
        user.setPassword("123");
        int count = userDAO.insertUser(user);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 测试根据Id修改User
     *
     * @throws SQLException
     */
    public static void testUpdate() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        User user = new User();
        user.setId(6);
        user.setUsername("林青霞");
        user.setPassword("456");
        int count = userDAO.updateUserById(user);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 测试根据id删除
     *
     * @throws SQLException
     */
    public static void testDelete() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        int count = userDAO.deleteUserById(3);
        System.out.println("受影响行数：" + count);
    }

    /**
     * 根据Id查询User信息
     */
    public static void selectUserById() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        User user = userDAO.selectUserById(5);
        System.out.println("查询结果：" + user);
    }

    /**
     * 测试查询所有结果
     */
    public static void testSelectAll() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        List<User> list = userDAO.selectAllUser();
        list.stream().forEach(System.out::println);
    }

    /**
     * 查询所有记录
     */
    public static void testSelectCount() throws SQLException {
        UserDAO userDAO = new UserDAOImpl();
        Long count = userDAO.selectCount();
        System.out.println("User表中有几条记录：" + count);
    }
}
